package se581.banker.processor;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Map;

import org.apache.log4j.Logger;

import se581.banker.BankerConstants;
import se581.banker.manager.AccountTracker;
import se581.banker.manager.AccountTransaction;
import se581.banker.util.OutputFileWriterUtil;

/**
 * Represents an Container in which the Tracking System will
 * run.
 * 
 * @author mmichalak
 *
 */
public class Processor implements Failoverable{
	static Logger logger = Logger.getLogger(Processor.class.getName());

	String id;
	AccountTracker udpateTracker;
	AccountTracker inquiryTracker;

	Map<String, ArrayList<AccountTransaction>> dataStore = null;
	
	OutputFileWriterUtil partialFileWriter = null;
	
	
	public Processor() {
		super();
	}
	
	public Processor(String id) {
		super();
		this.id = id;
	}
	
	
	public AccountTracker getUdpateTracker() {
		return udpateTracker;
	}
	public void setUdpateTracker(AccountTracker trackerA) {
		this.udpateTracker = trackerA;
	}
	public AccountTracker getInquiryTracker() {
		return inquiryTracker;
	}
	public void setInquiryTracker(AccountTracker trackerB) {
		this.inquiryTracker = trackerB;
	}
	public Map<String, ArrayList<AccountTransaction>> getDataStore() {
		return dataStore;
	}
	public void setDataStore(Map<String, ArrayList<AccountTransaction>> dataStore) {
		this.dataStore = dataStore;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}

	/**
	 * This is the less-freq full dump of datastore
	 * to another datastore.  It copies the checkpoint
	 * directly to the backup processor datastore
	 */
	public void executeFullCheckpoint() {

		/*		Map<String, ArrayList<AccountTransaction>> cloneDs = new HashMap<String, ArrayList<AccountTransaction>>();
		
		Iterator<String> iter = dataStore.keySet().iterator();
		while (iter.hasNext()) {
			String id = iter.next();
			ArrayList<AccountTransaction> locs = dataStore.get(id);
			@SuppressWarnings("unchecked")
			ArrayList<AccountTransaction> cloneLocs = (ArrayList<AccountTransaction>)locs.clone(); // clone list and Loc objs
			cloneDs.put(id, cloneLocs);
		}
		
		if (backupProc != null)
			 backupProc.setDataStore(cloneDs);*/

		// Save the object to file
	    FileOutputStream fos = null;
	    ObjectOutputStream out = null;
	    try {
	      fos = new FileOutputStream("c:\\SE480\\" + BankerConstants.CHECKPOINT_OUTPUT_FILENAME);
	      out = new ObjectOutputStream(fos);
	      
	      synchronized (dataStore) {
			out.writeObject(dataStore);
			
			OutputFileWriterUtil filewriter = new OutputFileWriterUtil(BankerConstants.CHECKPOINT_PARTIAL_FILENAME);
//			((UpdateTracker)this.udpateTracker).setPartialFileWriter(filewriter);
			setPartialFileWriter(filewriter);
	      }
	      
		out.close();
	    } catch (Exception ex) {
	      ex.printStackTrace();
	    }
	   /* // Read the object from file
	    // Save the object to file
	    FileInputStream fis = null;
	    ObjectInputStream in = null;
	    try {
	      fis = new FileInputStream(filename);
	      in = new ObjectInputStream(fis);
	      p = (Person) in.readObject();
	      out.close();
	    } catch (Exception ex) {
	      ex.printStackTrace();
	    }
	    System.out.println(p);*/
		
		logger.debug("Full Checkpoint completed...");
	}

	@Override
	public void executePartialCheckpoint() {
		// 1
		
	}

	@Override
	public void failover() {
		// TODO Auto-generated method stub
		
	}

	public OutputFileWriterUtil getPartialFileWriter() {
		return partialFileWriter;
	}

	public void setPartialFileWriter(OutputFileWriterUtil partialFileWriter) {
		this.partialFileWriter = partialFileWriter;
	}

	
}
